const jwt = require('jsonwebtoken')
const config = require('../secrets/jwt.config')

const userPayload = {
  iss: 'choice',
}

function createToken(data) {
  let opt = config.opt
  if (data.roles !== 'user') {
    opt.expiresIn = '2h'
  }
  return jwt.sign(
    Object.assign(userPayload, data),
    config.secret,
    opt)
}

function verifyToken(token) {
  return jwt.verify(
    token,
    config.secret)
}

module.exports = {
  createToken,
  verifyToken
}
